﻿using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationGenerator.Versions.BusinessLogic;

namespace ApplicationGenerator.Versions.Forms
{
    /// <summary>
    /// Kontrolka do wyświetlania wersji elementów
    /// </summary>
    public partial class VersionControl : UserControl
    {
        public VersionElement VersionType
        {
            get;
            set;
        }

        public VersionControl()
        {
            InitializeComponent();
        }

        public void InitializeVersionElements()
        {
            VersionDataAccess dataAccess = new VersionDataAccess(VersionType);
            IList<VersionInfo> versions = dataAccess.GetVersions();

            TreeNode rootNode = new TreeNode();
            rootNode.Text = GetVersionRootName();
            ctlVersionsTree.Nodes.Add(rootNode);

            foreach (var version in versions)
            {
                TreeNode newNode = new TreeNode();
                newNode.Text = version.Name;
                newNode.Tag = version;
                rootNode.Nodes.Add(newNode);
            }

        }

        private string GetVersionRootName()
        {
            switch (VersionType)
            {
                case VersionElement.Form:
                    {
                        return "Formularze";
                    }
                case VersionElement.List:
                    {
                        return "Listy";
                    }
                
                case VersionElement.Workflow:
                    {
                        return "Procesy";
                    }
                case VersionElement.Model:
                    {
                        return "Modele";
                    }
            }

            return "Brak";
        }

        public VersionInfo GetVersionInfo()
        {
            return ctlVersionsTree.SelectedNode.Tag as VersionInfo;
        }

        public bool IsVersionSelected()
        {
            if (ctlVersionsTree.SelectedNode != null
                && ctlVersionsTree.SelectedNode.Tag is VersionInfo)
            {
                return true;
            }

            return false;
        }
    }
}
